package cn.danger.com.fragment.memo;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.swipe.util.Attributes;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.bmob.v3.BmobObject;
import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.MainActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BaseFragment;
import cn.danger.com.model.entity.Memo;
import cn.danger.com.presenter.MemoPresenter;
import cn.danger.com.presenter.MemoPresenterImpl;
import cn.danger.com.util.AnimatorUtil;
import cn.danger.com.util.Constants;
import cn.danger.com.view.CommonView;
import cn.danger.com.view.adapter.MemoRecyclerViewAdapter;

/**
 * Created by 俊 on 2016/12/26.
 */
public class MemoFragment extends BaseFragment implements View.OnClickListener, CommonView {

    private View view;
    private MainActivity activity;
    private TextView bar_title_textView;
    private AppCompatButton edit_button;
    private RecyclerView memo_listView;
    private FloatingActionButton delete_button;
    private ProgressDialog dialog;
    private MemoPresenter presenter;
    private List<Memo> memos;
    private MemoRecyclerViewAdapter adapter;
    private MyReceiver receiver;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_memo, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (MainActivity) getActivity();
        bar_title_textView = activity.getBarTitleTextView();
        edit_button = activity.getEditButton();
        memo_listView = (RecyclerView) view.findViewById(R.id.memo_listView);
        delete_button = (FloatingActionButton) view.findViewById(R.id.delete_button);

        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("正在执行...");
        dialog.setCanceledOnTouchOutside(false);

        presenter = new MemoPresenterImpl(activity, this);

        memos = presenter.getMemos();
        if (memos.isEmpty()) {
            edit_button.setText("");
        } else {
            edit_button.setText("编辑");
        }

        memo_listView.setLayoutManager(new LinearLayoutManager(activity));

        adapter = new MemoRecyclerViewAdapter(activity, bar_title_textView, edit_button, delete_button, presenter);
        adapter.setMode(Attributes.Mode.Single);
        memo_listView.setAdapter(adapter);

        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter("update_memo_list");
        activity.registerReceiver(receiver, filter);
    }

    @Override
    public void bindEvent() {
        edit_button.setOnClickListener(this);
        delete_button.setOnClickListener(this);
        adapter.setOnItemClickListener(new MemoRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (edit_button.getText().equals("编辑")) {
                    Intent intent = new Intent(activity, CommonActivity.class);
                    intent.putExtra("target", "memo_detail");
                    intent.putExtra("memo", memos.get(position));
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edit_button:
                if (edit_button.getText().equals("编辑")) {
                    if (!(memos.isEmpty() || memos == null)) {
                        edit_button.setText("取消");
                    }
                    adapter.setList();
                    adapter.notifyDataSetChanged();
                } else {
                    if (!adapter.getCheckPositions().isEmpty()) {
                        bar_title_textView.setText("备忘录");
                    }
                    AnimatorUtil.setGoneAnimator(delete_button);
                    if (!(memos.isEmpty() || memos == null)) {
                        edit_button.setText("编辑");
                    }
                    adapter.setList();
                    adapter.notifyDataSetChanged();
                }
                break;
            case R.id.delete_button:
                deleteMemos();
                break;
            default:
                break;
        }
    }

    private void deleteMemos() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("提示");
        builder.setMessage("确定要删除所选项吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Set<Integer> positions = adapter.getCheckPositions();
                if (positions.isEmpty()) {
                    showMessage(Constants.PLEASE_CHOOSE_ITEM_TO_DELETE);
                } else {
                    List<BmobObject> delete_memos = new ArrayList<>();
                    for (Integer position : positions) {
                        delete_memos.add(memos.get(position));
                    }
                    presenter.deleteMemos(delete_memos);
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                edit_button.callOnClick();
                bar_title_textView.setText("备忘录");
            }
        });
        builder.create().show();
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
        if (message.equals(Constants.SUCCESS_TO_DELETE_MEMO) || message.equals(Constants.SUCCESS_TO_DELETE_LOCAL)) {
            memos = presenter.getMemos();
            adapter = new MemoRecyclerViewAdapter(activity, bar_title_textView, edit_button, delete_button, presenter);
            memo_listView.setAdapter(adapter);
            edit_button.callOnClick();
            if (edit_button.getText().equals("取消")) {
                edit_button.setText("编辑");
            }
            bar_title_textView.setText("备忘录");
            if (memos.isEmpty()) {
                edit_button.setText("");
            } else {
                edit_button.setText("编辑");
            }
        }
    }

    public void update() {
        memos = presenter.getMemos();
        if (memos.isEmpty()) {
            edit_button.setText("");
        } else {
            edit_button.setText("编辑");
        }
        Log.i("danger", "memos---->>" + memos.toString());
        adapter.setList();
        adapter.notifyDataSetChanged();
    }

    private class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            update();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            activity.unregisterReceiver(receiver);
        }
    }
}
